<?php
/***
 * 描述：订单管理[管理员权限]
 * @category		Admin		
 * @package			Action			
 * @subpackage			
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: OrderManageAction.class.php 52 2013-11-19 07:34:18Z whtaok@gmail.com $
 */
class OrderManageAction extends AdminAction {
	
	/**
	 *
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected  function _initialize() {
		parent::_initialize();
		$this->usingModel="OrderLib";
	}
	
	/**
	 * 订单查询管理首页面
	 * 默认查询的是处理中的订单
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct() {
		$search = array(
				'order_status'=> I('order_status'),
				'order_code'=> I('order_code'),
				'cust_mobile'=> I('cust_mobile'),
		);

		list($list,$page) = OrderModel::findListPageByOrderStatus($this->curr_shop_id,$search['order_status'],$search);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',$search);
		$this->display('index');
	}
	/**
	 * 显示已完成订单页面
	 * 默认查询的是处理中的订单
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showHistoryAct(){
		$search = array(
				'order_code'=> I('order_code'),
				'cust_mobile'=> I('cust_mobile'),
				'order_status'=> I('order_status'),
				'cust_order_status'=> I('cust_order_status'),
				'cust_order_time_start'=> I('cust_order_time_start',date('Y-m-d')),
				'cust_order_time_end'=> I('cust_order_time_end',date('Y-m-d')),
		);
		list($list,$page) = OrderModel::findListPageHistory($this->curr_shop_id,$search);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',$search);
		$this->display('list_history');
	}
	
	/**
	 * 订单状态审核了流程
	 * 1.处理中->配货中->配送中->交易完成
	 * 处理中： 客户可以取消订单（目前暂时不允许客户取下）
	 * 配货中：店员+权限的人可以操作；
	 * 配送中：店员+权限的人可以操作；配送员在配送系统中可以查看到自己相关的订单
	 * 交易完成：店员+权限的人可以操作；配送员也可以在配送系统中查看到自己相关的订单
	 * 
	 * 处理中->配货中：选择配送的商铺，修改订单状态为配货中
	 * 配货中->配送中：选择配送人，系统打印2张小票，一张店内用，一张客户用
	 * 配送中->订单结束:填写支付金额,根据实际情况修改客户订单状态(未退货,部分退货,全部退货)
	 * 对订单结束的,店长有一个审核,审核通过后才算交易结束
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showDetailAct() {
		$orderData = OrderModel::findDetail($this->curr_shop_id,I('order_code'));
		if($orderData){
			//设置当前步骤
			$steps = OrderModel::checkOrderStep($orderData[0]['order_status'],$orderData[0]['cust_order_status']);
			//设置是否可编辑商品退货
			if($orderData[0]['order_status']==4 && $orderData[0]['cust_order_status']==1){
				//支付完毕状态，并且是退货状态
				$orderData[0]['prod_cancel_edit']=1;	
			}else{
				$orderData[0]['prod_cancel_edit']=0;
			}
			//查询店铺信息
			list($shop,$users)=ShopModel::findDetailWithUsers($this->curr_shop_id);
			$this->assign('order',$orderData[0]);
			$this->assign('items',$orderData[1]);
			$this->assign('steps',$steps);
			$this->assign('shop',$shop);
			$this->assign('users',$users);
			$this->display('detail');
		}else{
			$this->error('订单不存在');
		}
	}
	/**
	 * 更改订单状态为打包状态
	 * 
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doReadyPackageAct(){
		$order_code = I('order_code');
		$upd_time = I('upd_time');
		$shop_saler=explode(',', I('shop_saler'),2);
		if(count($shop_saler)!=2){
			$this->error('请选择负责打包的店员');
			exit();
		}
		$orderData = OrderModel::findDetail($this->curr_shop_id,$order_code);
		if($orderData){
			$order = $orderData[0];
			if($order['order_status']==1 && $order['upd_time']==$upd_time){
				$data = array(
					'shop_saler_id'=>$shop_saler[0],
					'shop_saler_name'=>$shop_saler[1],
					'shop_saler_time'=>time(),
					'order_status'=>2,
					'upd_user'=>$this->curr_shop_id.'_'.$this->curr_user_id,
					'upd_time'=>time(),
				);
				$flag = OrderModel::updOrder($this->curr_shop_id,$order_code, $data);
				if($flag){
					$this->success("提交成功",U('showDetail',array('order_code'=>$order_code)));
				}else{
					$this->error("提交失败，请重新尝试");
				}
			}else{
				$this->error('订单状态异常，无法更新');
				exit();
			}
		}else{
			$this->error('订单不存在');
		}
		
	}
	
	/**
	 * 更改订单状态为配送状态
	 *
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doReadySendAct(){
		$order_code = I('order_code');
		$upd_time = I('upd_time');
		$shop_sender=explode(',', I('shop_sender'),2);
		if(count($shop_sender)!=2){
			$this->error('请选择负责配送的店员');
			exit();
		}
		$orderData = OrderModel::findDetail($this->curr_shop_id,$order_code);
		if($orderData){
			$order = $orderData[0];
			if($order['order_status']==2 && $order['upd_time']==$upd_time){
				$data = array(
						'shop_sender_id'=>$shop_sender[0],
						'shop_sender_name'=>$shop_sender[1],
						'shop_sender_time'=>time(),
						'order_status'=>3,
						'upd_user'=>$this->curr_shop_id.'_'.$this->curr_user_id,
						'upd_time'=>time(),
				);
				$flag = OrderModel::updOrder($this->curr_shop_id,$order_code, $data);
				if($flag){
					$this->success("提交成功",U('showDetail',array('order_code'=>$order_code)));
				}else{
					$this->error("提交失败，请重新尝试");
				}
			}else{
				$this->error('订单状态异常，无法更新');
				exit();
			}
		}else{
			$this->error('订单不存在');
		}
	
	}
	
	/**
	 * 更改订单状态为已支付状态，正常支付/部分退货/全部退货/订单取消
	 *
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doReadyPayAct(){
		$order_code = I('order_code');				//订单号
		$upd_time = I('upd_time');					//时间戳
		$cust_order_status = I('cust_order_status');//订单状态
		$orderData = OrderModel::findDetail($this->curr_shop_id,$order_code);
		if($orderData){
			$order = $orderData[0];
			if($order['order_status']!=0 && $order['upd_time']==$upd_time){
				$data = array(
						'order_status'=>4,
						'cust_order_status'=>$cust_order_status,
						'upd_user'=>$this->curr_shop_id.'_'.$this->curr_user_id,
						'upd_time'=>time(),
				);
				//判断更新的状态
				switch($cust_order_status){
					case 0:
						$data['cust_pay_amt'] = $order['order_amt']+$order['order_send_fee'];
						break;
					case 1:
						//部分退货：不做处理
						break;
					case 2:
						//全部退货：设置商品为全部退货状态
						$flag = OrderModel::cancelOrderItemAll($order_code);
						if(!flag){
							$this->error("全部退货操作失败，请重新尝试");
						}
						$data['cust_pay_amt'] = 0;
						break;
					case 3:
						//订单取消：设置商品为全部退货状态
						$flag = OrderModel::cancelOrderItemAll($order_code);
						if(!flag){
							$this->error("订单取消操作失败，请重新尝试");
						}
						$data['cust_pay_amt'] = 0;
						break;
					default:
						$this->error('订单状态不正确');
				}
				$flag = OrderModel::updOrder($this->curr_shop_id,$order_code, $data);
				if($flag){
					$this->success("提交成功",U('showDetail',array('order_code'=>$order_code)));
				}else{
					$this->error("提交失败，请重新尝试");
				}
			}else{
				$this->error('订单状态异常，无法更新');
				exit();
			}
		}else{
			$this->error('订单不存在');
		}
	}
	
	/**
	 * 结单
	 *
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doFinishOrderAct(){
		$order_code = I('order_code');				//订单号
		$upd_time = I('upd_time');					//时间戳
		$stock_back_flag = I('stock_back_flag');	//是否要入库 1：入库 0 不入库
		$orderData = OrderModel::findDetail($this->curr_shop_id,$order_code);
		if($orderData){
			$order = $orderData[0];
			//支付状态的单子才可以进行结单
			if($order['order_status']==4 && $order['upd_time']==$upd_time){
				$data = array(
						'order_status'=>0,
						'upd_user'=>$this->curr_shop_id.'_'.$this->curr_user_id,
						'upd_time'=>time(),
				);
				switch($order['cust_order_status']){
					case 0:
						//客户支付金额 = 订单总额+运费金额
						$data['cust_pay_amt'] = $order['order_amt']+$order['order_send_fee'];
						break;
					case 1:
						//部分退货：检查商品表是否处于部分退货中
						list($cancel_flag,$total_amt,$cancel_amt)=OrderModel::checkCancelOrder($order_code);
						//客户支付金额 = 总额-退货金额+运费金额
						$data['cust_pay_amt'] = $total_amt-$cancel_amt+$order['order_send_fee'];
						if(!$cancel_flag){
							$this->error('该订单未做退货处理，请重新确认！');
							exit();
						}
						break;
					case 2:
						//全部退货：检查商品表是否全部处于退货状态
						$data['cust_pay_amt'] = 0;
						list($cancel_flag,$total_amt,$cancel_amt)=OrderModel::checkCancelOrder($order_code);
						if(!$cancel_flag){
							$this->error('该订单未做退货处理，请重新确认！');
							exit();
						}
						break;
					case 3:
						//订单取消：检查商品表是否全部处于退货状态
						$data['cust_pay_amt'] = 0;
						list($cancel_flag,$total_amt,$cancel_amt)=OrderModel::checkCancelOrder($order_code);
						if(!$cancel_flag){
							$this->error('该订单未做退货处理，请重新确认！');
							exit();
						}
						break;
					default:
						$this->error('订单状态不正确');
				}
				$flag = OrderModel::updOrder($this->curr_shop_id,$order_code, $data);
				if($flag){
					$this->success("提交成功",U('showDetail',array('order_code'=>$order_code)));
				}else{
					$this->error("提交失败，请重新尝试");
				}
			}else{
				$this->error('订单状态异常，无法更新');
				exit();
			}
		}else{
			$this->error('订单不存在');
		}
	}
	
	/**
	 * 更改配送费用
	 *
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdSendFeeAct(){
		$order_code = I('order_code');
		$upd_time = I('upd_time');
		$order_send_fee = intval(I('order_send_fee'));
		$orderData = OrderModel::findDetail($this->curr_shop_id,$order_code);
		if($orderData){
			$order = $orderData[0];
			if($order['order_status']!=0 && $order['upd_time']==$upd_time){
				$data = array(
						'order_send_fee'=>$order_send_fee,
						'upd_user'=>$this->curr_shop_id.'_'.$this->curr_user_id,
						'upd_time'=>time(),
				);
				$flag = OrderModel::updOrder($this->curr_shop_id,$order_code, $data);
				if($flag){
					$this->success("提交成功",U('showDetail',array('order_code'=>$order_code)));
				}else{
					$this->error("提交失败，请重新尝试");
				}
			}else{
				$this->error('订单状态异常，无法更新');
				exit();
			}
		}else{
			$this->error('订单不存在');
		}
	
	}	
	
	/**
	 * 单品退货
	 *
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doCancelItemAct(){
		$order_code = I('order_code');
		$id = I('id');		
		$prod_cancel_num = intval(I('prod_cancel_num'));
		$orderData = OrderModel::findDetail($this->curr_shop_id,$order_code);
		if($orderData){
			if($orderData[0]['cust_order_status']==1){
				//部分退货才可以更改数量
				$flag = OrderModel::cancelOrderItem($order_code, $id, $prod_cancel_num);
				if($flag){
					$rtnData = array(
							'error'=>'0',
							'message'=>'退货成功',
					);
				}else{
					$rtnData = array(
							'error'=>'1',
							'message'=>'退货时发生错误，请重新尝试',
					);
				}
			}else{
				$rtnData = array(
						'error'=>'1',
						'message'=>'订单状态不正确，请确认后操作',
				);
			}

		}else{
			$rtnData = array(
					'error'=>'1',
					'message'=>'订单状态不正确，请确认后操作',
			);
		}

		$this->ajaxReturn($rtnData);
	}
	
	/**
	 * 更新订单备注
	 *
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdOrderBakAct(){
		$order_code = I('order_code');
		$order_bak = I('order_bak');
		$orderData = OrderModel::findDetail($this->curr_shop_id,$order_code);
		if($orderData){
			$data['order_bak'] = $order_bak.'('.date('Y-m-d').')';
			$flag = OrderModel::updOrder($this->curr_shop_id,$order_code, $data);
			if($flag){
				$rtnData = array(
						'error'=>'0',
						'message'=>'更新成功',
				);
			}else{
				$rtnData = array(
						'error'=>'1',
						'message'=>'更新备注发生错误，请重新尝试',
				);
			}
		}else{
			$rtnData = array(
					'error'=>'1',
					'message'=>'更新备注发生错误，请重新尝试',
			);
		}
		$this->ajaxReturn($rtnData);
	}
	/**
	 * 查询退货商品
	 *
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showProdCancelListAct(){
		$search= array(
			'order_code'	=> I('order_code'),	//按订单查询
			'prod_name'		=> I('prod_name'),	//按商品名称
			'prod_cancel_stock_status'=> I('prod_cancel_stock_status'),//退货入库情况
			'is_stock'=> I('is_stock'),			//是否退货
			
		);
		if($search['is_stock']==0){
			//只查询待入库的退货商品
			$search['prod_cancel_stock_status'] = 0;
		}
		list($list, $page)=OrderModel::findProdCancelList($this->curr_shop_id, $search);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->display('list_prodcancel');
		
	}
	/**
	 * 更新退货商品库存
	 *
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdStockAct(){
		$order_code = I('order_code');
		$id = I('id');
		$prod_cancel_stock_status = I('prod_cancel_stock_status');
		//查询商品状态是否正常
		$prod = OrderModel::findProdDetail($order_code, $id);
		if($prod['prod_cancel_stock_status']==0){
			//未处理的退货商品
			if($prod_cancel_stock_status == 1){
				//======================================入库操作
				//第一步：查询入库商品的库存ID
				$mod = new ProductLibModel();
				$prodStock = $mod->field('id, stock_prod_id')->find($prod['prod_id']);
				if($prodStock['stock_prod_id']==0){
					$stock_prod_id = $prodStock['id'];
				}else{
					$stock_prod_id = $prodStock['stock_prod_id'];
				}
				
				//第二步：更新商品订单的入库状态
				$orderDetailMod = new CommonModel('OrderDetail');
				$orderDetailMod->startTrans();
				$orderDetailMod->where(array('order_code'=>$order_code,'id'=>$id))->setField('prod_cancel_stock_status', 1);
				//第三步：入库
				$stockMod = new ProductPriceModel();
				$flag = $stockMod->addStockQty($this->curr_shop_id, $stock_prod_id, $prod['prod_num'],"【退货】$order_code");
				if($flag){
					$orderDetailMod->commit();
				}else{
					//失败回滚
					$orderDetailMod->rollback();
					$rtnData = array(
							'error'=>'1',
							'message'=>'操作失败',
					);
				}
			}else{
				//======================================不入库操作
				$orderDetailMod = new CommonModel('OrderDetail');
				$orderDetailMod->where(array('order_code'=>$order_code,'id'=>$id))->setField('prod_cancel_stock_status', 2);
			}
			if(!isset($rtnData)){
				$rtnData = array(
						'error'=>'0',
						'message'=>'操作成功',
				);
			}
		}else{
			//已处理过入库或不入库操作
			$rtnData = array(
					'error'=>'1',
					'message'=>'退货商品库存已处理过，请确认后在处理',
			);
		}
		$this->ajaxReturn($rtnData);
	}
}

?>